//专业信息管理
async function openCustomLayer(data) {
    // 构造自定义 HTML 内容
    var content = `  <form action="" class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">专业名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" id="major1">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">
           学院名称 
        </label>
        <div class="layui-input-block">
            <select id="selectMajor" style="display:block">
                <option value="">选择学院</option>
                
        `
       for (const key of data){
        content+=`<option value="${key.id}">`+key.collegename+'</option>'
       }
       content+=`</select></div></div></form>`
    layer.open({
        type: 1,
        title: '添加专业',
        content: content,
        area: ['400px', '250px'],
        btn: ['确定', '取消'],
        yes: async function(index, layero){
            // 获取用户输入的专业名称和选择的专业
            let inputMajor = $('#major1').val();
            if(inputMajor==''){
                layer.msg('输入的内容不能为空',{icon:2,time:1500})
                return;
            }
            let selectMajor = $('#selectMajor').val();
            selectMajor=parseInt(selectMajor)
            response=await taskObject.fetchtemplate(path='/api/admin/major',method='POST',data={'major':inputMajor,'collegeid':selectMajor})
            
            layer.close(index);
          
            setTimeout(()=>{
                if(response['code']!=200){
                    layer.msg(response['message'],{icon:2})
                }else{
                   setTimeout(()=>{
                    window.location.reload();         
                   },2000)
                   layer.msg('添加专业成功',{icon:1,time:2000})
                }
            },1200)
        },
        btn2: function(index, layero){
            
            return true;
        }
    });
}//添加专业


async function majorgetall(){
    response=await taskObject.fetchtemplate(path='/api/admin/major')
    return response
}
!(async function(){
    response=await majorgetall()
    dataform['majordata']=response.data
    await rendertable(data=response['data'],domhtml='.major tbody',domdata=['id','collegename','major','timestamp'])
   
    for (const i in response['data']){
         console.log(`.major tbody tr:eq(${i})`);
        $(`.major tbody tr:eq(${i})`).append(`<td><button class="layui-btn layui-bg-red " id=a${response['data'][i]['id']} onclick=deleteSchool(this)>删除</button></td>`)
    }
    $('.major i').on('click',async ()=>{
       await openCustomLayer(data=dataform['schooldata'])
    })
}())